<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];
	
	$dist = array();
	
	$bonus_date = date("Y-m-d",mktime(0, 0, 0 , date('m'), date('d')-1, date("Y")));
	$cdate = date('Y-m-d H:i:s');
					
	//$r=mysql_query("select * from gg_users where status='normal' and activated='y'");
	$r=mysql_query("select a.*,b.amount from gg_users a left join gg_member_cf_calc b on b.uid=a.id where a.status='normal' and a.activated='y' and date_format(a.cdate, '%Y-%m-%d')<='".$bonus_date."'");
	while($user = mysql_fetch_assoc($r))
	{
		if($user['matrix_position']==1)
			$dist[$user['matrix_upline']]['left']['id']=$user['id'];
		if($user['matrix_position']==2)
			$dist[$user['matrix_upline']]['right']['id']=$user['id'];
		
		$dist[$user['id']]['left']['bv']=0;
		$dist[$user['id']]['right']['bv']=0;
		$dist[$user['id']]['matrix_upline']=$user['matrix_upline'];
		$dist[$user['id']]['matrix_position']=$user['matrix_position'];
		$dist[$user['id']]['m1']=0;
		$dist[$user['id']]['m2']=0;
		$dist[$user['id']]['m3']=0;
		$dist[$user['id']]['s']=0;
		$dist[$user['id']]['j']=0;
		$dist[$user['id']]['w']=0;		
		$dist[$user['id']]['cf']=$user['amount']+0;
		$dist[$user['id']]['ps']=0;
		$dist[$user['id']]['pgs']=0;
		
		$dist[$user['id']]['jackpot'] = number_format(mysql_result(mysql_query("select volume_type from gg_member_comm where type='j' and uid=".$user['id']." order by volume_type desc limit 1"), 0)) or 0;
	}
	
	//for back date sponsor bonus
	$t1 = @mysql_num_rows($r1=mysql_query("select id,ref,count(*) as ttl from gg_users where sponsor_paid=0 and cdate<='2011-05-08 23:59:59' group by ref having ttl>=3"));
	if($t1>0){
		while($qualify = mysql_fetch_assoc($r1))
		{
			$t = @mysql_num_rows($r=mysql_query("select * from gg_member_comm where type='s' and percent in ('33.33','25.00') and uid=".$qualify['ref']));
			if($t>0){
				$dist[$qualify['ref']]['s']+=600;
				$sql = "INSERT INTO gg_member_comm SET uid=".$qualify['ref'].",from_uid=".$qualify['id'].",type='s',amount=600,percent='10.00',bonus_date='".$bonus_date."',cdate='".$cdate."'";
				mysql_query($sql);
				$sql = "UPDATE gg_users SET sponsor_paid=1 WHERE id=".$qualify['id'];
				mysql_query($sql);
			}else{
				$count=1;
				$rs_sponsor=mysql_query("select id,ref from gg_users where status='normal' and activated='y' and sponsor_paid=0 and ref=".$qualify['ref']);
				while($sponsor = mysql_fetch_assoc($rs_sponsor))
				{
					if($count>3){
						$dist[$sponsor['ref']]['s']+=600;
						$sql = "INSERT INTO gg_member_comm SET uid=".$sponsor['ref'].",from_uid=".$sponsor['id'].",type='s',amount=600,percent='10.00',bonus_date='".$bonus_date."',cdate='".$cdate."'";
					}else{
						$dist[$sponsor['ref']]['s']+=2000;
						$sql = "INSERT INTO gg_member_comm SET uid=".$sponsor['ref'].",from_uid=".$sponsor['id'].",type='s',amount=2000,percent='33.33',bonus_date='".$bonus_date."',cdate='".$cdate."'";
					}
					mysql_query($sql);
					$sql = "UPDATE gg_users SET sponsor_paid=1 WHERE id=".$sponsor['id'];
					mysql_query($sql);
					$count++;
				}
			}
		}
	}
		
	foreach ($dist as $distid=>$value) {
		if($value['m1']<=0) $value['m1'] = 0;
		if($value['m2']<=0) $value['m2'] = 0;
		if($value['m3']<=0) $value['m3'] = 0;
		if($value['s']<=0) $value['s'] = 0;
		if($value['j']<=0) $value['j'] = 0;
		$ttl_amount = $value['s'];
		
		if($ttl_amount>0){
			$sql = "UPDATE gg_member_comm_sum SET s=s+".$value['s']." WHERE uid=".$distid." AND bonus_date='".$bonus_date."'";
			mysql_query($sql);
			
			$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$distid), 0) or 0;
			$balance2=mysql_result(mysql_query("select mwallet from gg_users where id=".$distid), 0) or 0;
			
			$balance+=$ttl_amount;
			print_r("update gg_users set ewallet=".$balance." where id=".$distid."<br>");//mysql_query("update gg_users set ewallet=".$balance." where id=".$distid);
			
			$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='credit',amount=".$ttl_amount.",bal=".$balance.",descr='Daily Bonus ".$bonus_date."',cdate='".$cdate."'";
			mysql_query($sql);
	
			$maintain = $ttl_amount*0.3;
			$balance-=$maintain;
			$balance2+=$maintain;
			print_r("update gg_users set ewallet=".$balance.",mwallet=mwallet+".$maintain." where id=".$distid."<br>");//mysql_query("update gg_users set ewallet=".$balance.",mwallet=mwallet+".$maintain." where id=".$distid);
	
			$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='dedit',amount=".$maintain.",bal=".$balance.",descr='Auto Maintain ".$bonus_date.". ".$ttl_amount." X 30% = ".$maintain."',cdate='".$cdate."'";
			mysql_query($sql);
			$sql = "INSERT INTO gg_member_mwallet_record SET uid=".$distid.",type='credit',amount=".$maintain.",bal=".$balance2.",descr='Auto Maintain ".$bonus_date.". ".$ttl_amount." X 30% = ".$maintain."',cdate='".$cdate."'";
			mysql_query($sql);
			$sql = "INSERT INTO gg_member_maintain SET uid=".$distid.",credit=".$maintain.",rate=1,percent=30,amount=".$maintain.",bonus_date='".$bonus_date."',cdate='".$cdate."'";
			mysql_query($sql);
		}
		
		mysql_query("update gg_member_comm set status='confirmed' where bonus_date='".$bonus_date."'");
	}

	print_r("finish");
}
?>